const mongoose = require('mongoose');
const { User } = require('./db/user');

async function createUser() {
  try {
    console.log('开始创建用户...\n');
    
    // 连接数据库
    await mongoose.connect('mongodb+srv://2334231668:xin123666@cluster0.13va5ah.mongodb.net/2410B');
    console.log('✅ 数据库连接成功');
    
    // 检查用户是否已存在
    const userId = '68aeed055e85b6f7f7ff0480';
    const existingUser = await User.findById(userId);
    
    if (existingUser) {
      console.log('✅ 用户已存在:');
      console.log('- ID:', existingUser._id);
      console.log('- 用户名:', existingUser.username);
      console.log('- 头像:', existingUser.avatar);
      return;
    }
    
    // 创建新用户
    const newUser = new User({
      _id: new mongoose.Types.ObjectId(userId),
      username: 'admin',
      password: '123',
      avatar: 'https://picsum.photos/80',
      createdAt: new Date()
    });
    
    await newUser.save();
    console.log('✅ 用户创建成功:');
    console.log('- ID:', newUser._id);
    console.log('- 用户名:', newUser.username);
    console.log('- 头像:', newUser.avatar);
    
  } catch (error) {
    console.error('❌ 创建用户失败:', error.message);
  } finally {
    await mongoose.disconnect();
    console.log('\n数据库连接已关闭');
  }
}

// 运行脚本
createUser();
